package com.youlai.boot.modules.collect.controller;

import com.youlai.boot.modules.collect.service.CollectService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.modules.collect.model.form.CollectForm;
import com.youlai.boot.modules.collect.model.query.CollectQuery;
import com.youlai.boot.modules.collect.model.vo.CollectVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 用户收藏前端控制层
 *
 * @author maoyu
 * @since 2025-02-27 10:29
 */
@Tag(name = "用户收藏接口")
@RestController
@RequestMapping("/api/v1/collects")
@RequiredArgsConstructor
public class CollectController  {

    private final CollectService collectService;

    @Operation(summary = "用户收藏分页列表")
    @GetMapping("/page")    
    public PageResult<CollectVO> getCollectPage(CollectQuery queryParams ) {
        IPage<CollectVO> result = collectService.getCollectPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增用户收藏")
    @PostMapping    
    public Result<CollectVO> saveCollect(@RequestBody @Valid CollectForm formData ) {
        CollectVO result = collectService.saveCollect(formData);
        return Result.success(result);
    }

    @Operation(summary = "获取用户收藏表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('modules:collect:edit')")
    public Result<CollectForm> getCollectForm(
        @Parameter(description = "用户收藏ID") @PathVariable Long id
    ) {
        CollectForm formData = collectService.getCollectFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改用户收藏")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('modules:collect:edit')")
    public Result<Void> updateCollect(
            @Parameter(description = "用户收藏ID") @PathVariable Long id,
            @RequestBody @Validated CollectForm formData
    ) {
        boolean result = collectService.updateCollect(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除用户收藏")
    @DeleteMapping("/{ids}")    
    public Result<Void> deleteCollects(
        @Parameter(description = "用户收藏ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = collectService.deleteCollects(ids);
        return Result.judge(result);
    }
}
